﻿/////////////////////////////////////////////////
//文件名:User
//描  述:
//创建者:刘凡平(Iveely Liu)
//邮  箱:945222889@qq.com
//组  织:Iveely
//年  份:2012/5/11 15:38:11
///////////////////////////////////////////////


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IveelySE.BigData.Common;

namespace IveelySE.BigData.MasterNode
{
    /// <summary>
    /// 用户命令类（操作FileSystem的用户，传输控制命令）
    /// 命令监听端口：33
    /// </summary>
    public class UserCommand
    {
        /// <summary>
        /// 存储记录
        /// </summary>
        private static Storage Storage = new Storage();

        public static PObject dealWith(byte[] msg)
        {
            PObject obj = (PObject)Common.Serialize.Byte2Obj(msg);
            StoreNode node=Program.storeList.ChoseNode(obj);
            //Storage.Add(node, obj.fileName, obj.Id);
            //发送给子节点
            Common.Communication.Transfer.Client sendData = new Common.Communication.Transfer.Client(node.IP, Program.Config.MasterWritePort);
            PObject result=(PObject) sendData.ConnectToServer(obj);
            return new PObject("OK", "OK");
        }

        /// <summary>
        /// 开启命令监听
        /// </summary>
        public static void Listen()
        {
            byte[] msg = new byte[10240];
            Common.Communication.Transfer.Server userServer = new Common.Communication.Transfer.Server(Program.Config.IP, Program.Config.UserWritePort);
            while (true)
            {
                userServer.Listen(ref msg,dealWith);
            }
        }

  
    }
}
